<?php
require '../config.php';
$resultset = mysql_query($_REQUEST['query'],$conexion);

if(!$resultset)
{
    mysql_close();
    echo json_encode('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
    echo json_encode('Sin resultados');
}
else
{
	$row = mysql_fetch_assoc($resultset);
	echo   "<table class='table table-bordered'>";
	echo   "<tbody>";
	echo   "<tr>";
				echo   "<td>";
				echo "<form id='formulario' name='formulario'  style='background-color: #f2f9f2; text-align:left'>";
				echo "<div class='form_description'>";
				echo "<h2>Datos Generales</h2>";	
				echo "<ul>";
				echo "<li id='li_4' >";
				
				echo "<div>";
				echo "<label class='description' for='cedulaver'>C&eacute;dula </label>";
				echo "<input id='cedulaver' disabled type='text'  size='50' value='".$row['cedula']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='nombrever'>Nombre del cliente: </label>";
				echo "<input id='nombrever'  disabled   type='text'  size='50' value='".$row['nombres']." ".$row['apellidos']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='telefonover'>Tel&eacute;fono: </label>";
				echo "<input id='telefonover'  disabled  type='text'  size='50' value='".$row['telefono']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='telefonover2'>M&oacute;vil: </label>";
				echo "<input id='telefonover2'  disabled  type='text' size='50' value='".$row['telefono_movil']."'>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='emailver'>Email: </label>";
				echo "<input id='emailver' disabled type='text'  size='50' value='".$row['email']."'>";
				//echo "<button type='button' class='btn btn-link btn-xs'   onclick='enviaremail(". $row['idclientes'] .")'><span class='glyphicon glyphicon-envelope'></span> Enviar mensaje</button>";
				echo "</div>";
				
				echo "<div>";
				echo "<label class='description' for='cumplever'>Cumplea&ntilde;os: </label>";
				
				echo "<input id='cumplever' disabled type='text'  size='50' value='".date("d M Y",strtotime($row['fecha_nacimiento']))."'>";
						
				echo "</div>";
				
				echo 	"<div>";
				echo 	"<label class='description' for='direccionver'>Direcci&oacute; del cliente: </label>";
				echo 	"<input id='direccionver'  disabled   type='text'  size='75' value='".retornaProvincia($row['provincia'])." - ".$row['direccion']."'>";
				echo 	"</div>";
				echo 	"<div>";
				echo 	"<label class='description' for='profesionver'>Profesi&oacute;n del cliente: </label>";
				echo 	"<input id='profesionver'  disabled   type='text'  size='50' value='".$row['profesion']."'>";
				echo 	"</div>";
				echo 	"<div>";
				
				$result2 = mysql_query("select p.actualizar,p.insertar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre = 'Clientes'",$conexion);
				if(mysql_num_rows($result2)>0)
				{
					$row2 = mysql_fetch_assoc($result2);
					if($row2['actualizar']== 1)
					{
						echo 	"<button type='button' class='btn btn-link btn-xs'  onclick='modificar(". $row['idclientes'] .")'><span class='glyphicon glyphicon-pencil'></span> Modificar datos</button>";	
					}
					if($row2['insertar']== 1 )
					{
						echo   	"<button type='button' class='btn btn-link btn-xs'  onclick='regdonacion2(". $row['idclientes'] .")'><img src='../imagenes/glyphicons_037_coins.png' width='18' height='16' /> Registrar donaci&oacute;n</button>";
						echo   	"<button type='button' class='btn btn-link btn-xs'  onclick='regllamada2(". $row['idclientes'] .")'><img src='../imagenes/glyphicons_442_earphone.png' width='18' height='16' /> Registrar llamada</button>";
					
					}
				}
				
				
				
				
				
				echo 	"</div>";
				echo   "</li>";
				echo   "</ul>";
				echo   "</form>";
				echo   "</td>";
				echo   "<td >";
						$resultset2 = mysql_query("select fecha,concat('$',FORMAT(monto,2)) as monto from regpagos where idcliente='".$row['idclientes']."' order by fecha desc limit 5",$conexion);
						echo   "<table class='table table-bordered' style='background-color: #f2f9f2'>";
						echo   "<caption><strong>Donaciones (&uacute;ltimos 5)</strong></caption>";
						echo   "<tr class='success'>";
						echo   "<th>Fecha</th><th>Monto</th>";
						echo   "</tr>";
						while($row2 = mysql_fetch_assoc($resultset2))
						{
							echo   "<tr>";
							echo   "<td>".date("F j, Y",strtotime($row2['fecha']))."</td>";
							echo   "<td>".$row2['monto']."</td>";
							echo   "</tr>";
						}
							echo   "<tr class='warning'>";
							echo   "<td colspan='2'><button type='button' class='btn btn-link btn-xs' onclick='verHistorialPagos(". $row['idclientes'] .");' ><span class='glyphicon glyphicon-eye-open'></span> Ver detalles</button> </td>";
							echo   "</tr>";
						echo   "<tbody>";
						echo   "</tbody>";
						echo   "</table>";
							
							$resultset3 = mysql_query("select fecha,efectividad from regllamadas where idcliente='".$row['idclientes']."' order by fecha desc limit 5",$conexion);
							echo   "<table class='table table-bordered' style='background-color: #f2f9f2'>";
								echo   "<caption><strong>Llamadas (&uacute;ltimos 5)</strong></caption>";
								echo   "<tr class='success'>";
								echo   "<th>Fecha</th><th>Efectividad</th>";
								echo   "</tr>";
								while($row3 = mysql_fetch_assoc($resultset3))
								{
									echo   "<tr>";
									echo   "<td>".date("F j, Y",strtotime($row3['fecha']))."</td>";
									if($row3['efectividad']==1)
										echo   "<td><span class='glyphicon glyphicon-ok'></span></td>";
									else 
										echo   "<td><span class='glyphicon glyphicon-minus'></span></td>";
									echo   "</tr>";
								}
								echo   "<tr class='warning'>";
								echo   "<td colspan='2'><button type='button' class='btn btn-link btn-xs' onclick='verHistorialLLamadas(". $row['idclientes'] .");' ><span class='glyphicon glyphicon-eye-open'></span> Ver detalles</button> </td>";
								echo   "</tr>";
								echo   "<tbody>";
								echo   "</tbody>";
								echo   "</table>";
							
				echo   "</td>";
			echo   "</tr>";
			echo   "</tbody>";
			echo   "</table>";
			
	mysql_close();
		
}

function retornaCategoria($codigo){
	$resultset2 = mysql_query("select descripcion from categoriasclientes where idcategoriasclientes= ".$codigo);
	$row2 = mysql_fetch_assoc($resultset2);
	return $row2['descripcion'];
}
function retornaProvincia($codigo){
	$resultset2 = mysql_query("select PROVINCIA from provincias where idprovincias= ".$codigo);
	$row2 = mysql_fetch_assoc($resultset2);
	return $row2['PROVINCIA'];
}

?>